package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/jmoiron/sqlx"
	"vip_video/configx"
	"vip_video/internal/library"
	"vip_video/internal/logic"
	"vip_video/types"
)

type UserController struct {
	Db     *sqlx.DB
	Config *configx.Config
}

func NewUserController() *UserController {
	return &UserController{}
}

func (uc *UserController) WithDb(db *sqlx.DB) *UserController {
	uc.Db = db
	return uc
}

func (uc *UserController) WithConfig(config *configx.Config) *UserController {
	uc.Config = config
	return uc
}

func (uc *UserController) MobileLogin(ctx *gin.Context) {
	var request *types.MobileLoginRequest
	response := library.NewResponse(ctx)
	if err := ctx.ShouldBind(&request); err != nil {
		response.Error(err.Error())
		return
	}
	user, err := logic.NewUserLogic(uc.Db).
		WithConfig(uc.Config).
		WithCtx(ctx).
		MobileLogin(request.Mobile, request.Captcha)
	if err != nil {
		response.Error(err.Error())
		return
	}
	response.Success("success", user)
	return
}

func (uc *UserController) UserInfo(ctx *gin.Context) {
	response := library.NewResponse(ctx)
	response.Success("success", library.AuthLibraryInstance(uc.Db).GetUserInfo())
	return
}
